using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for processOrder
/// </summary>
public class processOrder:DBConnectionTool
{
	public processOrder() :base()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public string getOrderID()
    {
        OpenCn();
        SqlCommand cmd = new SqlCommand("select right(max(OrderID),5) from Orders", cn);
        String s = cmd.ExecuteScalar().ToString();
        Int32 result;
        if (Int32.TryParse(s, out result))
            result++;
        else
            result = 1;
        int l = result.ToString().Length;
        int count = 5 - l;
        string sresult = "Ord";
        for (int i = 0; i < count; i++)
        {
            sresult = sresult + "0";
        }
        sresult = sresult + result;
        CloseCn();
        return sresult;
    }

    public string getOrderIDByUserandProduct(string UserID, string ProductID)
    {
        OpenCn();
        SqlCommand cmd = new SqlCommand("select Orders.OrderID from Orders,OrderDetails,Products where Orders.OrderID=OrderDetails.OrderID and OrderDetails.ProductID = Products.ProductID and Products.ProductID='"+ProductID+"' and Orders.UserID='"+UserID+"'", cn);
        string result = cmd.ExecuteScalar().ToString();
        CloseCn();
        return result;
    }
   
    public string getOrderStatus(string OrderID)
    {
        OpenCn();        
        SqlCommand cmd = new SqlCommand("Select OrderStatus from Orders where OrderID = '"+OrderID+"'", cn);
        string result = cmd.ExecuteScalar().ToString();        
        CloseCn();
        return result;
    }
    
    public DataTable getOrderByProductID(string ProductID)
    {
        OpenCn();
        SqlDataAdapter da = new SqlDataAdapter("select * from Orders,OrderDetails where Orders.OrderID = OrderDetails.OrderID and ProductID = '" + ProductID + "'", cn);
        DataTable dt = new DataTable("Orders");
        da.Fill(dt);
        CloseCn();
        return dt;
    }

    public DataTable getOrderByUserID(string UserID)
    {
        OpenCn();
        SqlDataAdapter da = new SqlDataAdapter("select OrderDetails.OrderNumber,OrderDetails.OrderID,Products.ProductID, ProductImage, ProductName, ProductPrice, OrderDetails.Quantity, OrderDetails.Price from Products, Orders, OrderDetails where Products.ProductID = OrderDetails.ProductID and Orders.OrderID = OrderDetails.OrderID and UserID = '" + UserID + "' order by OrderDetails.OrderID desc", cn);
        DataTable dt = new DataTable("Orders");
        da.Fill(dt);
        CloseCn();
        return dt;
    }

    public DataTable getOrderByUserID1(string UserID)
    {
        OpenCn();
        SqlDataAdapter da = new SqlDataAdapter("select Orders.OrderID from Orders where UserID = '" + UserID + "' order by Orders.OrderID desc", cn);
        DataTable dt = new DataTable("Orders");
        da.Fill(dt);
        CloseCn();
        return dt;
    }

    public DataTable getcart(string UserID)
    {
        OpenCn();
        SqlDataAdapter da = new SqlDataAdapter("select * from Products, Orders, OrderDetails where Products.ProductID = OrderDetails.ProductID and Orders.OrderID = OrderDetails.OrderID", cn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        CloseCn();
        return dt;        
    }

        
    public int insertOrder(string OrderID, DateTime OrderDate, string ReceiverName, string OrderAddress, string OrderStatus, string UserID, string PaymentTypeID, string DeliveryTypeID)
    {
        OpenCn();
        SqlCommand cmd = new SqlCommand("Insert Into Orders(OrderID,OrderDate,ReceiverName,OrderAddress,OrderStatus,UserID,PaymentTypeID,DeliveryTypeID) Values(@OrderID,@OrderDate,@ReceiverName,@OrderAddress,@OrderStatus,@UserID,@PaymentTypeID,@DeliveryTypeID)", cn);
        cmd.Parameters.Add(new SqlParameter("@OrderID", SqlDbType.Char, 16));
        cmd.Parameters.Add(new SqlParameter("@OrderDate", SqlDbType.SmallDateTime));
        cmd.Parameters.Add(new SqlParameter("@ReceiverName", SqlDbType.NVarChar, 200));
        cmd.Parameters.Add(new SqlParameter("@OrderAddress", SqlDbType.NVarChar, 500));
        cmd.Parameters.Add(new SqlParameter("@OrderStatus", SqlDbType.NVarChar, 200));
        cmd.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Char, 7));
        cmd.Parameters.Add(new SqlParameter("@PaymentTypeID", SqlDbType.Char, 5));
        cmd.Parameters.Add(new SqlParameter("@DeliveryTypeID", SqlDbType.Char, 5));
        cmd.Parameters["@OrderID"].Value = OrderID;
        cmd.Parameters["@OrderDate"].Value = OrderDate;
        cmd.Parameters["@ReceiverName"].Value = ReceiverName;
        cmd.Parameters["@OrderAddress"].Value = OrderAddress;
        cmd.Parameters["@OrderStatus"].Value = OrderStatus;
        cmd.Parameters["@UserID"].Value = UserID;
        cmd.Parameters["@PaymentTypeID"].Value = PaymentTypeID;
        cmd.Parameters["@DeliveryTypeID"].Value = DeliveryTypeID;        
        int result = cmd.ExecuteNonQuery();
        CloseCn();
        return result;
    }

    public int updateOrder(string OrderID, string ReceiverName, string OrderAddress, string PaymentTypeID, string DeliveryTypeID)
    {
        OpenCn();
        SqlCommand cmd = new SqlCommand("Update Orders set ReceiverName=@ReceiverName,OrderAddress=@OrderAddress, PaymentTypeID=@PaymentTypeID, DeliveryTypeID=@DeliveryTypeID where OrderID=@OrderID", cn);
        cmd.Parameters.Add(new SqlParameter("@OrderID", SqlDbType.Char, 8));
        cmd.Parameters.Add(new SqlParameter("@ReceiverName", SqlDbType.NVarChar, 200));
        cmd.Parameters.Add(new SqlParameter("@OrderAddress", SqlDbType.NVarChar, 500));
        cmd.Parameters.Add(new SqlParameter("@PaymentTypeID", SqlDbType.Char, 5));
        cmd.Parameters.Add(new SqlParameter("@DeliveryTypeID", SqlDbType.Char, 5));
        cmd.Parameters["@OrderID"].Value = OrderID;
        cmd.Parameters["@ReceiverName"].Value = ReceiverName;
        cmd.Parameters["@OrderAddress"].Value = OrderAddress;
        cmd.Parameters["@PaymentTypeID"].Value = PaymentTypeID;
        cmd.Parameters["@DeliveryTypeID"].Value = DeliveryTypeID;
        int result = cmd.ExecuteNonQuery();
        CloseCn();
        return result;
    }

    public int updateOrderinAdmin(string OrderID, string OrderStatus)
    {
        OpenCn();
        SqlCommand cmd = new SqlCommand("Update Orders set OrderStatus=@OrderStatus where OrderID=@OrderID", cn);
        cmd.Parameters.Add(new SqlParameter("@OrderID", SqlDbType.Char, 8));
        cmd.Parameters.Add(new SqlParameter("@OrderStatus", SqlDbType.NVarChar, 200));       
        cmd.Parameters["@OrderID"].Value = OrderID;
        cmd.Parameters["@OrderStatus"].Value = OrderStatus;        
        int result = cmd.ExecuteNonQuery();
        CloseCn();
        return result;
    }
    //public int updateOrder(string OrderID, string ReceiverName, string OrderAddress)
    //{
    //    OpenCn();
    //    SqlCommand cmd = new SqlCommand("Update Orders set ReceiverName=@ReceiverName,OrderAddress=@OrderAddress where OrderID=@OrderID", cn);
    //    cmd.Parameters.Add(new SqlParameter("@OrderID", SqlDbType.Char, 8));
    //    cmd.Parameters.Add(new SqlParameter("@ReceiverName", SqlDbType.VarChar, 200));
    //    cmd.Parameters.Add(new SqlParameter("@OrderAddress", SqlDbType.VarChar, 500));
        
    //    cmd.Parameters["@OrderID"].Value = OrderID;
    //    cmd.Parameters["@ReceiverName"].Value = ReceiverName;
    //    cmd.Parameters["@OrderAddress"].Value = OrderAddress;
        
    //    int result = cmd.ExecuteNonQuery();
    //    CloseCn();
    //    return result;
    //}

    public int deleteOrder(string OrderID)
    {
        OpenCn();
        SqlCommand cmd = new SqlCommand("delete Orders where OrderID = @OrderID", cn);
        cmd.Parameters.Add(new SqlParameter("@OrderID", SqlDbType.Char, 8));
        cmd.Parameters["@OrderID"].Value = OrderID;
        int result = cmd.ExecuteNonQuery();
        CloseCn();
        return result;
    }

    public DataTable listOders()
    {
        OpenCn();
        DataTable dt = new DataTable("Orders");

        SqlDataAdapter da = new SqlDataAdapter("select * from Orders", cn);
        da.Fill(dt);
        CloseCn();
        return dt;
    }

    public DataTable getOrderIDNewest(string UserID)
    {
        OpenCn();
        SqlDataAdapter da = new SqlDataAdapter("select top 1 Orders.OrderID from Orders where UserID='"+UserID+"' order by OrderID desc", cn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        CloseCn();
        return dt;
    }

}
